<!DOCTYPE HTML>
<html lang="en-US" manifest="../manifest.appcache">
    
    <head prefix="og: http://ogp.me/ns# book: http://ogp.me/ns/book#">
        
        
        <meta charset="UTF-8">
        <title>《The Swift Programming Language》中文版</title>

        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="robots" content="index, follow">
        <meta name="author" content="">
        <meta name="description" content="Swift 中文翻译组：364279588（要求对翻译感兴趣）">
        <meta name="keywords" content="gitbook,github" >
        <meta name="generator" content="www.gitbook.io">

        
        <link rel="next" href="../chapter3/05_Declarations.html" />
        
        
        <link rel="prev" href="../chapter3/04_Expressions.html" />
        

        <meta property="og:title" content="语句 | 这一次，让中国和世界同步">
        <meta property="og:site_name" content="这一次，让中国和世界同步">
        <meta property="og:type" content="book">
        <meta property="og:locale" content="en_US">

        <meta property="book:author" content="https://github.com/">
        <meta property="book:tag" content="GitBook">

        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">

        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
        
    </head>
    <body>
        
        

        <link rel="stylesheet" href="../gitbook/style.css">
        


        
    <div class="book"  data-level="3.5" data-basepath=".." data-revision="1402523087598">
    <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    
    <a href="https://github.com/null" target="_blank" class="btn pull-left home-bookmark" aria-label="GitHub home"><i class="fa fa-bookmark-o"></i></a>
    
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    <span id="font-settings-wrapper">
        <a href="#" class="btn pull-left toggle-font-settings" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="btn-group btn-block">
        <button id="reduce-font-size" class="btn btn-default">A</button>
        <button id="enlarge-font-size" class="btn btn-default">A</button>
    </div>

    <ul class="list-group font-family-list">
        <li class="list-group-item" data-font="0">Serif</li>
        <li class="list-group-item" data-font="1">Sans</li>
    </ul>

    <div class="btn-group btn-group-xs btn-block color-theme-list">
        <button type="button" class="btn btn-default" id="color-theme-preview-0" data-theme="0">White</button>
        <button type="button" class="btn btn-default" id="color-theme-preview-1" data-theme="1">Sepia</button>
        <button type="button" class="btn btn-default" id="color-theme-preview-2" data-theme="2">Night</button>
    </div>
</div>

    </span>

    <!-- Actions Right -->
    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    

    

    <!-- Title -->
    <h1>
        <i class="fa fa-spinner fa-spin"></i>
        <a href="../" >这一次，让中国和世界同步</a>
    </h1>
</div>

    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Search" class="form-control" />
    </div>
    <ul class="summary">
        
        

        

        

        

        <li data-level="0" data-path="index.html">
            <a href="../"><i class="fa fa-check"></i> 序</a>
        </li>
        
    
        <li class="chapter " data-level="1" data-path="chapter1/chapter1.html">
            
            <a href="../chapter1/chapter1.html">
                <i class="fa fa-check"></i> <b>1.</b> 欢迎使用 Swift
            </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="chapter1/01_swift.html">
            
            <a href="../chapter1/01_swift.html">
                <i class="fa fa-check"></i> <b>1.1.</b> 关于 Swift
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="chapter1/02_a_swift_tour.html">
            
            <a href="../chapter1/02_a_swift_tour.html">
                <i class="fa fa-check"></i> <b>1.2.</b> Swift 初见
            </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter2/chapter2.html">
            
            <a href="../chapter2/chapter2.html">
                <i class="fa fa-check"></i> <b>2.</b> Swift 教程
            </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="chapter2/01_The_Basics.html">
            
            <a href="../chapter2/01_The_Basics.html">
                <i class="fa fa-check"></i> <b>2.1.</b> 基础部分
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
            
            <a href="../chapter2/02_Basic_Operators.html">
                <i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="chapter2/03_Strings_and_Characters.html">
            
            <a href="../chapter2/03_Strings_and_Characters.html">
                <i class="fa fa-check"></i> <b>2.3.</b> 字符串和字符
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="chapter2/04_Collection_Types.html">
            
            <a href="../chapter2/04_Collection_Types.html">
                <i class="fa fa-check"></i> <b>2.4.</b> 集合类型
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="chapter2/05_Control_Flow.html">
            
            <a href="../chapter2/05_Control_Flow.html">
                <i class="fa fa-check"></i> <b>2.5.</b> 控制流
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="chapter2/06_Functions.html">
            
            <a href="../chapter2/06_Functions.html">
                <i class="fa fa-check"></i> <b>2.6.</b> 函数
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="chapter2/07_Closures.html">
            
            <a href="../chapter2/07_Closures.html">
                <i class="fa fa-check"></i> <b>2.7.</b> 闭包
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="chapter2/08_Enumerations.html">
            
            <a href="../chapter2/08_Enumerations.html">
                <i class="fa fa-check"></i> <b>2.8.</b> 枚举
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="chapter2/09_Classes_and_Structures.html">
            
            <a href="../chapter2/09_Classes_and_Structures.html">
                <i class="fa fa-check"></i> <b>2.9.</b> 类和结构体
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="chapter2/10_Properties.html">
            
            <a href="../chapter2/10_Properties.html">
                <i class="fa fa-check"></i> <b>2.10.</b> 属性
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="chapter2/11_Methods.html">
            
            <a href="../chapter2/11_Methods.html">
                <i class="fa fa-check"></i> <b>2.11.</b> 方法
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
            
            <a href="../chapter2/12_Subscripts.html">
                <i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="chapter2/13_Inheritance.html">
            
            <a href="../chapter2/13_Inheritance.html">
                <i class="fa fa-check"></i> <b>2.13.</b> 继承
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
            
            <a href="../chapter2/14_Initialization.html">
                <i class="fa fa-check"></i> <b>2.14.</b> 构造过程
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
            
            <a href="../chapter2/15_Deinitialization.html">
                <i class="fa fa-check"></i> <b>2.15.</b> 析构过程
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.16" data-path="chapter2/16_Automatic_Reference_Counting.html">
            
            <a href="../chapter2/16_Automatic_Reference_Counting.html">
                <i class="fa fa-check"></i> <b>2.16.</b> 自动引用计数
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.17" data-path="chapter2/17_Optional_Chaining.html">
            
            <a href="../chapter2/17_Optional_Chaining.html">
                <i class="fa fa-check"></i> <b>2.17.</b> 可选链
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.18" data-path="chapter2/18_Type_Casting.html">
            
            <a href="../chapter2/18_Type_Casting.html">
                <i class="fa fa-check"></i> <b>2.18.</b> 类型检查
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.19" data-path="chapter2/19_Nested_Types.html">
            
            <a href="../chapter2/19_Nested_Types.html">
                <i class="fa fa-check"></i> <b>2.19.</b> 类型嵌套
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.20" data-path="chapter2/20_Extensions.html">
            
            <a href="../chapter2/20_Extensions.html">
                <i class="fa fa-check"></i> <b>2.20.</b> 扩展
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.21" data-path="chapter2/21_Protocols.html">
            
            <a href="../chapter2/21_Protocols.html">
                <i class="fa fa-check"></i> <b>2.21.</b> 协议
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.22" data-path="chapter2/22_Generics.html">
            
            <a href="../chapter2/22_Generics.html">
                <i class="fa fa-check"></i> <b>2.22.</b> 泛型
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.23" data-path="chapter2/23_Advanced_Operators.html">
            
            <a href="../chapter2/23_Advanced_Operators.html">
                <i class="fa fa-check"></i> <b>2.23.</b> 高级操作符
            </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter3/chapter3.html">
            
            <a href="../chapter3/chapter3.html">
                <i class="fa fa-check"></i> <b>3.</b> 语言参考
            </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="chapter3/01_About_the_Language_Reference.html">
            
            <a href="../chapter3/01_About_the_Language_Reference.html">
                <i class="fa fa-check"></i> <b>3.1.</b> 关于语言参考
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="chapter3/02_Lexical_Structure.html">
            
            <a href="../chapter3/02_Lexical_Structure.html">
                <i class="fa fa-check"></i> <b>3.2.</b> 词法结构
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="chapter3/03_Types.html">
            
            <a href="../chapter3/03_Types.html">
                <i class="fa fa-check"></i> <b>3.3.</b> 类型
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="chapter3/04_Expressions.html">
            
            <a href="../chapter3/04_Expressions.html">
                <i class="fa fa-check"></i> <b>3.4.</b> 表达式
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="chapter3/10_Statements.html">
            
            <a href="../chapter3/10_Statements.html">
                <i class="fa fa-check"></i> <b>3.5.</b> 语句
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="chapter3/05_Declarations.html">
            
            <a href="../chapter3/05_Declarations.html">
                <i class="fa fa-check"></i> <b>3.6.</b> 声明
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="chapter3/06_Attributes.html">
            
            <a href="../chapter3/06_Attributes.html">
                <i class="fa fa-check"></i> <b>3.7.</b> 特性
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="chapter3/07_Patterns.html">
            
            <a href="../chapter3/07_Patterns.html">
                <i class="fa fa-check"></i> <b>3.8.</b> 模式
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="chapter3/08_Generic_Parameters_and_Arguments.html">
            
            <a href="../chapter3/08_Generic_Parameters_and_Arguments.html">
                <i class="fa fa-check"></i> <b>3.9.</b> 泛型参数
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="chapter3/09_Summary_of_the_Grammar.html">
            
            <a href="../chapter3/09_Summary_of_the_Grammar.html">
                <i class="fa fa-check"></i> <b>3.10.</b> 语法总结
            </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
        </li>
<li style="margin-left:15%;"> <iframe  src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
  allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="page-wrapper" tabindex="-1">
                <div class="book-progress">
    <div class="bar">
        <div class="inner" style="width: 89.47368421052632%;min-width: 86.84210526315789%;"></div>
    </div>
    <div class="chapters">
    
        <a href="../index.html" title="Introduction" class="chapter done new-chapter" data-progress="0" style="left: 0%;"></a>
    
        <a href="../chapter1/chapter1.html" title="欢迎使用 Swift" class="chapter done new-chapter" data-progress="1" style="left: 2.6315789473684212%;"></a>
    
        <a href="../chapter1/01_swift.html" title="关于 Swift" class="chapter done " data-progress="1.1" style="left: 5.2631578947368425%;"></a>
    
        <a href="../chapter1/02_a_swift_tour.html" title="Swift 初见" class="chapter done " data-progress="1.2" style="left: 7.894736842105263%;"></a>
    
        <a href="../chapter2/chapter2.html" title="Swift 教程" class="chapter done new-chapter" data-progress="2" style="left: 10.526315789473685%;"></a>
    
        <a href="../chapter2/01_The_Basics.html" title="基础部分" class="chapter done " data-progress="2.1" style="left: 13.157894736842104%;"></a>
    
        <a href="../chapter2/10_Properties.html" title="属性" class="chapter done " data-progress="2.10" style="left: 15.789473684210526%;"></a>
    
        <a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
    
        <a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
    
        <a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
    
        <a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 26.31578947368421%;"></a>
    
        <a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 28.94736842105263%;"></a>
    
        <a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 31.57894736842105%;"></a>
    
        <a href="../chapter2/17_Optional_Chaining.html" title="可选链" class="chapter done " data-progress="2.17" style="left: 34.21052631578947%;"></a>
    
        <a href="../chapter2/18_Type_Casting.html" title="类型检查" class="chapter done " data-progress="2.18" style="left: 36.8421052631579%;"></a>
    
        <a href="../chapter2/19_Nested_Types.html" title="类型嵌套" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
    
        <a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
    
        <a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
    
        <a href="../chapter2/21_Protocols.html" title="协议" class="chapter done " data-progress="2.21" style="left: 47.36842105263158%;"></a>
    
        <a href="../chapter2/22_Generics.html" title="泛型" class="chapter done " data-progress="2.22" style="left: 50%;"></a>
    
        <a href="../chapter2/23_Advanced_Operators.html" title="高级操作符" class="chapter done " data-progress="2.23" style="left: 52.63157894736842%;"></a>
    
        <a href="../chapter2/03_Strings_and_Characters.html" title="字符串和字符" class="chapter done " data-progress="2.3" style="left: 55.26315789473684%;"></a>
    
        <a href="../chapter2/04_Collection_Types.html" title="集合类型" class="chapter done " data-progress="2.4" style="left: 57.89473684210526%;"></a>
    
        <a href="../chapter2/05_Control_Flow.html" title="控制流" class="chapter done " data-progress="2.5" style="left: 60.526315789473685%;"></a>
    
        <a href="../chapter2/06_Functions.html" title="函数" class="chapter done " data-progress="2.6" style="left: 63.1578947368421%;"></a>
    
        <a href="../chapter2/07_Closures.html" title="闭包" class="chapter done " data-progress="2.7" style="left: 65.78947368421052%;"></a>
    
        <a href="../chapter2/08_Enumerations.html" title="枚举" class="chapter done " data-progress="2.8" style="left: 68.42105263157895%;"></a>
    
        <a href="../chapter2/09_Classes_and_Structures.html" title="类和结构体" class="chapter done " data-progress="2.9" style="left: 71.05263157894737%;"></a>
    
        <a href="../chapter3/chapter3.html" title="语言参考" class="chapter done new-chapter" data-progress="3" style="left: 73.6842105263158%;"></a>
    
        <a href="../chapter3/01_About_the_Language_Reference.html" title="关于语言参考" class="chapter done " data-progress="3.1" style="left: 76.3157894736842%;"></a>
    
        <a href="../chapter3/09_Summary_of_the_Grammar.html" title="语法总结" class="chapter done " data-progress="3.10" style="left: 78.94736842105263%;"></a>
    
        <a href="../chapter3/02_Lexical_Structure.html" title="词法结构" class="chapter done " data-progress="3.2" style="left: 81.57894736842105%;"></a>
    
        <a href="../chapter3/03_Types.html" title="类型" class="chapter done " data-progress="3.3" style="left: 84.21052631578948%;"></a>
    
        <a href="../chapter3/04_Expressions.html" title="表达式" class="chapter done " data-progress="3.4" style="left: 86.84210526315789%;"></a>
    
        <a href="../chapter3/10_Statements.html" title="语句" class="chapter done " data-progress="3.5" style="left: 89.47368421052632%;"></a>
    
        <a href="../chapter3/05_Declarations.html" title="声明" class="chapter  " data-progress="3.6" style="left: 92.10526315789474%;"></a>
    
        <a href="../chapter3/06_Attributes.html" title="特性" class="chapter  " data-progress="3.7" style="left: 94.73684210526316%;"></a>
    
        <a href="../chapter3/07_Patterns.html" title="模式" class="chapter  " data-progress="3.8" style="left: 97.36842105263158%;"></a>
    
        <a href="../chapter3/08_Generic_Parameters_and_Arguments.html" title="泛型参数" class="chapter  " data-progress="3.9" style="left: 100%;"></a>
    
    </div>
</div>

                <div class="page-inner">
                
                    <section class="normal" id="section-gitbook_6535">
                    
                        <blockquote>
<p>翻译：coverxit</p>
<p>校对：numbbbbb</p>
</blockquote>
<h1 id="-">语句</h1>
<hr>
<p>本页包含内容：</p>
<ul>
<li><a href="#loop_statements">循环语句</a></li>
<li><a href="#branch_statements">分支语句</a></li>
<li><a href="#labeled_statement">带标签的语句</a></li>
<li><a href="#control_transfer_statements">控制传递语句</a></li>
</ul>
<p>在 Swift 中，有两种类型的语句：简单语句和控制流语句。简单语句是最常见的，用于构造表达式和声明。控制流语句则用于控制程序执行的流程，Swift 中有三种类型的控制流语句：循环语句、分支语句和控制传递语句。</p>
<p>循环语句用于重复执行代码块；分支语句用于执行满足特定条件的代码块；控制传递语句则用于修改代码的执行顺序。在稍后的叙述中，将会详细地介绍每一种类型的控制流语句。</p>
<p>是否将分号(;)添加到语句的结尾处是可选的。但若要在同一行内写多条独立语句，请务必使用分号。</p>
<blockquote>
<p>GRAMMAR OF A STATEMENT</p>
<p><em>statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a><strong>;</strong> <em>opt</em></p>
<p><em>statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration" target="_blank"><em>declaration</em></a><strong>;</strong> <em>opt</em></p>
<p><em>statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement" target="_blank"><em>loop-statement</em></a><strong>;</strong> <em>opt</em></p>
<p><em>statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/branch-statement" target="_blank"><em>branch-statement</em></a><strong>;</strong> <em>opt</em></p>
<p><em>statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/labeled-statement" target="_blank"><em>labeled-statement</em></a></p>
<p><em>statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/control-transfer-statement" target="_blank"><em>control-transfer-statement</em></a><strong>;</strong> <em>opt</em></p>
<p><em>statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement" target="_blank"><em>statment</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements" target="_blank"><em>statements</em></a><strong>;</strong> <em>opt</em></p>
</blockquote>
<p><a name="loop_statements"></a></p>
<h2 id="-">循环语句</h2>
<p>取决于特定的循环条件，循环语句允许重复执行代码块。Swift 提供四种类型的循环语句：<code>for</code>语句、<code>for-in</code>语句、<code>while</code>语句和<code>do-while</code>语句。</p>
<p>通过<code>break</code>语句和<code>continue</code>语句可以改变循环语句的控制流。有关这两条语句，请参考<a href="#break_statement">Break 语句</a>和<a href="#continue_statement">Continue 语句</a>。</p>
<blockquote>
<p>GRAMMAR OF A LOOP STATEMENT</p>
<p><em>loop-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-statement" target="_blank"><em>for-statement</em></a></p>
<p><em>loop-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-in-statement" target="_blank"><em>for-in-statement</em></a></p>
<p><em>loop-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-statement" target="_blank"><em>while-statement</em></a></p>
<p><em>loop-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/do-while-statement" target="_blank"><em>do-while-statement</em></a></p>
</blockquote>
<h3 id="for-">For 语句</h3>
<p><code>for</code>语句允许在重复执行代码块的同时，递增一个计数器。</p>
<p><code>for</code>语句的形式如下：</p>
<pre><code class="lang-swift">for `initialzation`; `condition`; `increment` {
    `statements`
}
</code></pre>
<p><em>initialzation</em>、<em>condition</em>和<em>increment</em>之间的分号，以及包围循环体<em>statements</em>的大括号都是不可省略的。</p>
<p><code>for</code>语句的执行流程如下：</p>
<ol>
<li><p><em>initialzation</em>只会被执行一次，通常用于声明和初始化在接下来的循环中需要使用的变量。</p>
</li>
<li><p>计算<em>condition</em>表达式：
 如果为真(<code>true</code>)，<em>statements</em>将会被执行，然后转到第3步。如果为假(<code>false</code>)，<em>statements</em>和<em>increment</em>都不会被执行，<code>for</code>至此执行完毕。</p>
</li>
<li><p>计算<em>increment</em>表达式，然后转到第2步。</p>
</li>
</ol>
<p>定义在<em>initialzation</em>中的变量仅在<code>for</code>语句的作用域以内有效。<em>condition</em>表达式的值的类型必须遵循<code>LogicValue</code>协议。</p>
<blockquote>
<p>GRAMMAR OF A FOR STATEMENT</p>
<p><em>for-statement</em> → <strong>for</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init" target="_blank"><em>for-init</em></a> <em>opt</em> <strong>;</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em> <strong>;</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a></p>
<p><em>for-statement</em> → <strong>for (</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init" target="_blank"><em>for-init</em></a> <em>opt</em> <strong>;</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em> <strong>;</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em> <strong>)</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a></p>
<p><em>for-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration" target="_blank"><em>variable-declaration</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-list" target="_blank"><em>expression-list</em></a></p>
</blockquote>
<h3 id="for-in-">For-In 语句</h3>
<p><code>for-in</code>语句允许在重复执行代码块的同时，迭代集合(或遵循<code>Sequence</code>协议的任意类型)中的每一项。</p>
<p><code>for-in</code>语句的形式如下：</p>
<pre><code class="lang-swift">for `item` in `collection` {
    `statements`
}
</code></pre>
<p><code>for-in</code>语句在循环开始前会调用<em>collection</em>表达式的<code>generate</code>方法来获取一个生成器类型（这是一个遵循<code>Generator</code>协议的类型）的值。接下来循环开始，调用<em>collection</em>表达式的<code>next</code>方法。如果其返回值不是<code>None</code>，它将会被赋给<em>item</em>，然后执行<em>statements</em>，执行完毕后回到循环开始处；否则，将不会赋值给<em>item</em>也不会执行<em>statements</em>，<code>for-in</code>至此执行完毕。</p>
<blockquote>
<p>GRAMMAR OF A FOR-IN STATEMENT</p>
<p><em>for-in-statement</em> → <strong>for</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern" target="_blank"><em>pattern</em></a> <strong>in</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a></p>
</blockquote>
<h3 id="while-">While 语句</h3>
<p><code>while</code>语句允许重复执行代码块。</p>
<p><code>while</code>语句的形式如下：</p>
<pre><code class="lang-swift">while `condition` {
    `statements`
}
</code></pre>
<p><code>while</code>语句的执行流程如下：</p>
<ol>
<li><p>计算<em>condition</em>表达式：
 如果为真(<code>true</code>)，转到第2步。如果为假(<code>false</code>)，<code>while</code>至此执行完毕。</p>
</li>
<li><p>执行<em>statements</em>，然后转到第1步。</p>
</li>
</ol>
<p>由于<em>condition</em>的值在<em>statements</em>执行前就已计算出，因此<code>while</code>语句中的<em>statements</em>可能会被执行若干次，也可能不会被执行。</p>
<p><em>condition</em>表达式的值的类型必须遵循<code>LogicValue</code>协议。同时，<em>condition</em>表达式也可以使用可选绑定，请参考<a href="">可选绑定<code>待添加链接</code></a>。</p>
<blockquote>
<p>GRAMMAR OF A WHILE STATEMENT</p>
<p><em>while-statement</em> → <strong>while</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition" target="_blank"><em>while-condition</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a></p>
<p><em>while-condition</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration" target="_blank"><em>declaration</em></a></p>
</blockquote>
<h3 id="do-while-">Do-While 语句</h3>
<p><code>do-while</code>语句允许代码块被执行一次或多次。</p>
<p><code>do-while</code>语句的形式如下：</p>
<pre><code class="lang-swift">do {
    `statements`
} while `condition`
</code></pre>
<p><code>do-while</code>语句的执行流程如下：</p>
<ol>
<li><p>执行<em>statements</em>，然后转到第2步。</p>
</li>
<li><p>计算<em>condition</em>表达式：
 如果为真(<code>true</code>)，转到第1步。如果为假(<code>false</code>)，<code>do-while</code>至此执行完毕。</p>
</li>
</ol>
<p>由于<em>condition</em>表达式的值是在<em>statements</em>表达式执行后才计算出，因此<em>do-while</em>语句中的<em>statements</em>至少会被执行一次。</p>
<p><em>condition</em>表达式的值的类型必须遵循<code>LogicValue</code>协议。同时，<em>condition</em>表达式也可以使用可选绑定，请参考<a href="">可选绑定<code>待添加链接</code></a>。</p>
<blockquote>
<p>GRAMMAR OF A DO-WHILE STATEMENT</p>
<p><em>do-while-statement</em> → <strong>do</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a> <strong>while</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition" target="_blank"><em>while-condition</em></a></p>
</blockquote>
<p><a name="branch_statements"></a></p>
<h2 id="-">分支语句</h2>
<p>取决于一个或者多个条件的值，分支语句允许程序执行指定部分的代码。显然，分支语句中条件的值将会决定如何分支以及执行哪一块代码。Swift 提供两种类型的分支语句：<code>if</code>语句和<code>switch</code>语句。</p>
<p><code>switch</code>语句中的控制流可以用<code>break</code>语句修改，请参考<a href="#break_statement">Break 语句</a>。</p>
<blockquote>
<p>GRAMMAR OF A BRANCH STATEMENT</p>
<p><em>branch-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement" target="_blank"><em>if-statement</em></a></p>
<p><em>branch-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement" target="_blank"><em>switch-statement</em></a></p>
</blockquote>
<h3 id="if-">If 语句</h3>
<p>取决于一个或多个条件的值，<code>if</code>语句将决定执行哪一块代码。</p>
<p><code>if</code>语句有两种标准形式，在这两种形式里都必须有大括号。</p>
<p>第一种形式是当且仅当条件为真时执行代码，像下面这样：</p>
<pre><code class="lang-swift">if `condition` {
    `statements`
}
</code></pre>
<p>第二种形式是在第一种形式的基础上添加<em>else语句</em>，当只有一个<em>else语句</em>时，像下面这样：</p>
<pre><code class="lang-swift">if `condition` {
    `statements to execute if condition is true`
} else {
    `statements to execute if condition is false`
}
</code></pre>
<p>同时，<em>else语句</em>也可包含<code>if</code>语句，从而形成一条链来测试更多的条件，像下面这样：</p>
<pre><code class="lang-swift">if `condition 1` {
    `statements to execute if condition 1 is true`
} else if `condition 2` {
    `statements to execute if condition 2 is true`
}
else {
    `statements to execute if both conditions are false`
}
</code></pre>
<p><code>if</code>语句中条件的值的类型必须遵循<code>LogicValue</code>协议。同时，条件也可以使用可选绑定，请参考<a href="">可选绑定<code>待添加链接</code></a>。</p>
<blockquote>
<p>GRAMMAR OF AN IF STATEMENT</p>
<p><em>if-statement</em> → <strong>if</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-condition" target="_blank"><em>if-condition</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/else-clause" target="_blank"><em>else-clause</em></a> <em>opt</em></p>
<p><em>if-condition</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration" target="_blank"><em>declaration</em></a></p>
<p><em>else-clause</em> → <strong>else</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block" target="_blank"><em>code-block</em></a> | <strong>else</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement" target="_blank"><em>if-statement</em></a> <em>opt</em></p>
</blockquote>
<h3 id="switch-">Switch 语句</h3>
<p>取决于<code>switch</code>语句的控制表达式(<em>control expression</em>)，<code>switch</code>语句将决定执行哪一块代码。</p>
<p><code>switch</code>语句的形式如下：</p>
<pre><code class="lang-swift">switch `control expression` {
    case `pattern 1`:
        `statements`
    case `pattern 2` where `condition`:
        `statements`
    case `pattern 3` where `condition`,
    `pattern 4` where `condition`:
        `statements`
    default:
        `statements`
}
</code></pre>
<p><code>switch</code>语句的<em>控制表达式(control expression)</em>会首先被计算，然后与每一个<em>case</em>的模式(pattern)进行匹配。如果匹配成功，程序将会执行对应的<em>case</em>块里的<em>statements</em>。另外，每一个<em>case</em>块都不能为空，也就是说在每一个<em>case</em>块中至少有一条语句。如果你不想在匹配到的<em>case</em>块中执行代码，只需在块里写一条<code>break</code>语句即可。</p>
<p>可以用作控制表达式的值是十分灵活的，除了标量类型(scalar types，如<code>Int</code>、<code>Character</code>)外，你可以使用任何类型的值，包括浮点数、字符串、元组、自定义类的实例和可选(optional)类型，甚至是枚举类型中的成员值和指定的范围(range)等。关于在<code>switch</code>语句中使用这些类型，请参考<a href="../chapter2/05_Control_Flow.html">控制流</a>一章的<a href="../chapter2/05_Control_Flow.html#switch">Switch</a>。</p>
<p>你可以在模式后面添加一个起保护作用的表达式(guard expression)。<em>起保护作用的表达式</em>是这样构成的：关键字<code>where</code>后面跟着一个作为额外测试条件的表达式。因此，当且仅当<em>控制表达式</em>匹配一个<em>case</em>的某个模式且起保护作用的表达式为真时，对应<em>case</em>块中的<em>statements</em>才会被执行。在下面的例子中，<em>控制表达式</em>只会匹配含两个相等元素的元组，如<code>(1, 1)</code>：</p>
<pre><code class="lang-swift">case let (x, y) where x == y:
}
</code></pre>
<p>正如上面这个例子，也可以在模式中使用<code>let</code>（或<code>var</code>）语句来绑定常量（或变量）。这些常量（或变量）可以在其对应的起保护作用的表达式和其对应的<em>case</em>块里的代码中引用。但是，如果<em>case</em>中有多个模式匹配控制表达式，那么这些模式都不能绑定常量（或变量）。</p>
<p><code>switch</code>语句也可以包含默认(<code>default</code>)块，只有其它<em>case</em>块都无法匹配控制表达式时，默认块中的代码才会被执行。一个<code>switch</code>语句只能有一个默认块，而且必须在<code>switch</code>语句的最后面。</p>
<p>尽管模式匹配操作实际的执行顺序，特别是模式的计算顺序是不可知的，但是 Swift 规定<code>switch</code>语句中的模式匹配的顺序和书写源代码的顺序保持一致。因此，当多个模式含有相同的值且能够匹配控制表达式时，程序只会执行源代码中第一个匹配的<em>case</em>块中的代码。</p>
<h4 id="switch-">Switch 语句必须是完备的</h4>
<p>在 Swift 中，<code>switch</code>语句中控制表达式的每一个可能的值都必须至少有一个<em>case</em>块与之对应。在某些情况下（例如，表达式的类型是<code>Int</code>），你可以使用默认块满足该要求。</p>
<h4 id="-fall-through-">不存在隐式的贯穿(fall through)</h4>
<p>当匹配的<em>case</em>块中的代码执行完毕后，程序会终止<code>switch</code>语句，而不会继续执行下一个<em>case</em>块。这就意味着，如果你想执行下一个<em>case</em>块，需要显式地在你需要的<em>case</em>块里使用<code>fallthrough</code>语句。关于<code>fallthrough</code>语句的更多信息，请参考<a href="#fallthrough_statement">Fallthrough 语句</a>。</p>
<blockquote>
<p>GRAMMAR OF A SWITCH STATEMENT</p>
<p><em>switch-statement</em> → <strong>switch</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <strong>{</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases" target="_blank"><em>switch-cases</em></a> <em>opt</em> <strong>}</strong></p>
<p><em>switch-cases</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-case" target="_blank"><em>switch-case</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases" target="_blank"><em>switch-cases</em></a> <em>opt</em></p>
<p><em>switch-case</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label" target="_blank"><em>case-label</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements" target="_blank"><em>statement</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label" target="_blank"><em>default-label</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements" target="_blank"><em>statements</em></a></p>
<p><em>switch-case</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label" target="_blank"><em>case-label</em></a> <strong>;</strong> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label" target="_blank"><em>default-label</em></a> <strong>;</strong></p>
<p><em>case-label</em> → <strong>case</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list" target="_blank"><em>case-item-list</em></a> <strong>:</strong></p>
<p><em>case-item-list</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern" target="_blank"><em>pattern</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause" target="_blank"><em>guard-clause</em></a> <em>opt</em> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern" target="_blank"><em>pattern</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause" target="_blank"><em>guard-clause</em></a> <em>opt</em>, <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list" target="_blank"><em>case-item-list</em></a></p>
<p><em>default-label</em> → <strong>default :</strong></p>
<p><em>guard-clause</em> → <strong>where</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-expression" target="_blank"><em>guard-expression</em></a></p>
<p><em>guard-expression</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a></p>
</blockquote>
<p><a name="labeled_statement"></a>
<a name="control_transfer_statements"></a> 带标签的语句</p>
<p>你可以在循环语句或<code>switch</code>语句前面加上<em>标签</em>，它由标签名和紧随其后的冒号(:)组成。在<code>break</code>和<code>continue</code>后面跟上标签名可以显式地在循环语句或<code>switch</code>语句中更改控制流，把控制权传递给指定标签标记的语句。关于这两条语句用法，请参考<a href="#break_statement">Break 语句</a>和<a href="#continue_statement">Continue 语句</a>。</p>
<p>标签的作用域是该标签所标记的语句之后的所有语句。你可以不使用带标签的语句，但只要使用它，标签名就必唯一。</p>
<p>关于使用带标签的语句的例子，请参考<a href="../chapter2/05_Control_Flow.html">控制流</a>一章的<a href="">带标签的语句<code>待添加链接</code></a>。</p>
<blockquote>
<p>GRAMMAR OF A LABELED STATEMENT</p>
<p><em>labeled-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label" target="_blank"><em>statement-label</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement" target="_blank"><em>loop-statement</em></a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label" target="_blank"><em>statement-label</em></a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement" target="_blank"><em>switch-statement</em></a></p>
<p><em>statement-label</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name" target="_blank"><em>label-name</em></a> <strong>:</strong></p>
<p><em>label-name</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier" target="_blank"><em>identifier</em></a></p>
</blockquote>
<h2 id="-">控制传递语句</h2>
<p>通过无条件地把控制权从一片代码传递到另一片代码，控制传递语句能够改变代码执行的顺序。Swift 提供四种类型的控制传递语句：<code>break</code>语句、<code>continue</code>语句、<code>fallthrough</code>语句和<code>return</code>语句。</p>
<blockquote>
<p>GRAMMAR OF A CONTROL TRANSER STATEMENT</p>
<p><em>control-transfer-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/break-statement" target="_blank"><em>break-statement</em></a></p>
<p><em>control-transfer-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/continue-statement" target="_blank"><em>continue-statement</em></a></p>
<p><em>control-transfer-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/fallthrough-statement" target="_blank"><em>fallthrough-statement</em></a></p>
<p><em>control-transfer-statement</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/return-statement" target="_blank"><em>return-statement</em></a></p>
</blockquote>
<p><a name="break_statement"></a></p>
<h3 id="break-">Break 语句</h3>
<p><code>break</code>语句用于终止循环或<code>switch</code>语句的执行。使用<code>break</code>语句时，可以只写<code>break</code>这个关键词，也可以在<code>break</code>后面跟上标签名(label name)，像下面这样：</p>
<pre><code class="lang-swift">break
break `label name`
</code></pre>
<p>当<code>break</code>语句后面带标签名时，可用于终止由这个标签标记的循环或<code>switch</code>语句的执行。</p>
<p>而当只写<code>break</code>时，则会终止<code>switch</code>语句或上下文中包含<code>break</code>语句的最内层循环的执行。</p>
<p>在这两种情况下，控制权都会被传递给循环或<code>switch</code>语句外面的第一行语句。</p>
<p>关于使用<code>break</code>语句的例子，请参考<a href="../chapter2/05_Control_Flow.html">控制流</a>一章的<a href="">Break<code>待添加链接</code></a>和<a href="">带标签的语句<code>待添加链接</code></a>。</p>
<blockquote>
<p>GRAMMAR OF A BREAK STATEMENT</p>
<p><em>break-statement</em> → <strong>break</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name" target="_blank"><em>label-name</em></a> <em>opt</em></p>
</blockquote>
<p><a name="continue_statement"></a></p>
<h3 id="continue-">Continue 语句</h3>
<p><code>continue</code>语句用于终止循环中当前迭代的执行，但不会终止该循环的执行。使用<code>continue</code>语句时，可以只写<code>continue</code>这个关键词，也可以在<code>continue</code>后面跟上标签名(label name)，像下面这样：</p>
<pre><code class="lang-swift">continue
continue `label name`
</code></pre>
<p>当<code>continue</code>语句后面带标签名时，可用于终止由这个标签标记的循环中当前迭代的执行。</p>
<p>而当只写<code>break</code>时，可用于终止上下文中包含<code>continue</code>语句的最内层循环中当前迭代的执行。</p>
<p>在这两种情况下，控制权都会被传递给循环外面的第一行语句。</p>
<p>在<code>for</code>语句中，<code>continue</code>语句执行后，<em>increment</em>表达式还是会被计算，这是因为每次循环体执行完毕后<em>increment</em>表达式都会被计算。</p>
<p>关于使用<code>continue</code>语句的例子，请参考<a href="../chapter2/05_Control_Flow.html">控制流</a>一章的<a href="">Continue<code>待添加链接</code></a>和<a href="">带标签的语句<code>待添加链接</code></a>。</p>
<blockquote>
<p>GRAMMAR OF A CONTINUE STATEMENT</p>
<p><em>continue-statement</em> → <strong>continue</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name" target="_blank"><em>label-name</em></a> <em>opt</em></p>
</blockquote>
<p><a name="fallthrough_statement"></a></p>
<h3 id="fallthrough-">Fallthrough 语句</h3>
<p><code>fallthrough</code>语句用于在<code>switch</code>语句中传递控制权。<code>fallthrough</code>语句会把控制权从<code>switch</code>语句中的一个<em>case</em>传递给下一个<em>case</em>。这种传递是无条件的，即使下一个<em>case</em>的值与<code>switch</code>语句的控制表达式的值不匹配。</p>
<p><code>fallthrough</code>语句可出现在<code>switch</code>语句中的任意<em>case</em>里，但不能出现在最后一个<em>case</em>块中。同时，<code>fallthrough</code>语句也不能把控制权传递给使用了可选绑定的<em>case</em>块。</p>
<p>关于在<code>switch</code>语句中使用<code>fallthrough</code>语句的例子，请参考<a href="../chapter2/05_Control_Flow.html">控制流</a>一章的<a href="">控制传递语句<code>待添加链接</code></a>。</p>
<blockquote>
<p>GRAMMAR OF A FALLTHROUGH STATEMENT</p>
<p><em>continue-statement</em> → <strong>fallthrough</strong></p>
</blockquote>
<h3 id="return-">Return 语句</h3>
<p><code>return</code>语句用于在函数或方法的实现中将控制权传递给调用者，接着程序将会从调用者的位置继续向下执行。</p>
<p>使用<code>return</code>语句时，可以只写<code>return</code>这个关键词，也可以在<code>return</code>后面跟上表达式，像下面这样：</p>
<pre><code class="lang-swift">return
return `expression`
</code></pre>
<p>当<code>return</code>语句后面带表达式时，表达式的值将会返回给调用者。如果表达式值的类型与调用者期望的类型不匹配，Swift 则会在返回表达式的值之前将表达式值的类型转换为调用者期望的类型。</p>
<p>而当只写<code>return</code>时，仅仅是将控制权从该函数或方法传递给调用者，而不返回一个值。（这就是说，该函数或方法的返回类型为<code>Void</code>或<code>()</code>）</p>
<blockquote>
<p>GRAMMAR OF A RETURN STATEMENT</p>
<p><em>return-statement</em> → <strong>return</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank"><em>expression</em></a> <em>opt</em></p>
</blockquote>

                    
                    </section>
                
                </div>
            </div>
        </div>

        
        <a href="../chapter3/04_Expressions.html" class="navigation navigation-prev " aria-label="Previous page: 表达式"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter3/05_Declarations.html" class="navigation navigation-next " aria-label="Next page: 声明"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        

        <script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
        <script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
        <script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
        <script src="../gitbook/app.js"></script>
        

    
    <script src="../gitbook/plugins/gitbook-plugin-mixpanel/plugin.js"></script>
    

    
    <script src="http://cdn.mathjax.org/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
